<?php

namespace app\index\controller;

use app\common\CommonResponse;
use app\common\model\Work;
use think\App;
use think\facade\Log;
use think\facade\Session;
use think\Request;

class User extends Base
{
    protected $userModel;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->userModel=new \app\common\model\User();
    }


    /** 用户基本信息
     * @param Request $request
     * @return mixed|void
     */
    public function profile(Request $request){
        if($request->isPost()){
            $user=$request->post();
            $updateUserInfo = $this->userModel->allowField(['username','realname','email','remark'])->save($user, ['id' => $this->user['id']]);
            if(!$updateUserInfo){
                return $this->error("用户信息修改失败");
            }
            return $this->success("用户信息修改成功");
        }
        $this->assign("user",$this->user);
        return $this->fetch();
    }

    /** 更改头像
     * @param Request $request
     * @return CommonResponse
     */
    public function avatar(Request $request){
        $user=$request->post();
        if($user['avatar']==""){
            return CommonResponse::error("参数不能为空");
        }
        $oldUser=$this->userModel::get($this->user['id']);
        $oldUser->avatar=$user['avatar'];
        $updateAvatar = $oldUser->save($user);
        if(!$updateAvatar){
            return CommonResponse::error("更新头像失败");
        }
        return CommonResponse::success("更新头像成功");
    }

    /** 修改密码
     * @param Request $request
     * @return mixed|void
     */
    public function editPwd(Request $request){
        if($request->isPost()){
            $user=$request->post();
            if(md5($user['oldpwd'])!=$this->user['password']){
                return $this->error("旧密码不正确");
            }
            if($user['newpwd']!=$user['confirmpwd']){
                return $this->error("两次密码不一致");
            }
            $res = $this->userModel->save(['password'=>md5($user['newpwd'])], ['id' => $this->user['id']]);
            if(!$res){
                return $this->error("密码修改失败");
            }
            Session::clear();
            return $this->success("密码修改成功");
        }
        return $this->fetch();
    }

    /** 用户列表
     * @return mixed
     */
    public function user(){
        $users = $this->userModel->order("id desc")->paginate(10);
        $this->assign("users",$users);
        return $this->fetch();
    }

    /** 删除用户
     * @return mixed
     */
    public function delUser(Request $request){
        if($this->user['state']!=3){
           return json(CommonResponse::error("无权执行此操作"));
        }
        if(!$request->post("id")){
           return json(CommonResponse::error("用户id为空"));
        }
        $userId=$request->post("id");
        $res = $this->userModel::get($userId)->delete();
        if(!$res){
            return json(CommonResponse::error("用户删除失败"));
        }
        Log::info("用户{userId}被删除",['userId'=>$userId]);
        return json(CommonResponse::success("用户删除成功"));
    }

    /** 设置为评审教师
     *
     */
    public function powerTeacher(Request $request){
        if($this->user['state']!=3){
            return json(CommonResponse::error("无权执行此操作"));
        }
        if(!$request->post("id")){
            return json(CommonResponse::error("用户id为空"));
        }
        $userId=$request->post("id");
        $user = $this->userModel::get($userId);
        $user->state=3;
        $saveRes = $user->save();
        if(!$saveRes){
            return json(CommonResponse::error("评审老师设置失败"));
        }
        return json(CommonResponse::success("设置成功"));
    }



}